import { Dimensions } from "@sapi-game/constants";
import { Vector3Utils } from "@sapi-game/utils";
import { PillarOfFortuneModule } from "../module";
import { POFStatic } from "../static";
import { PillarOfFortuneMainState } from "./mainState";

export class PillarOfFortuneWatingState extends PillarOfFortuneModule.State {
    override onEnter(): void {
        this.tpToStart();
        this.runner.run(async (r) => {
            this.context.players.title("地图:" + this.context.map.name);
            await r.wait(40);
            for (let i = 5; i > 0; i--) {
                this.context.players.title(i.toString());
                await r.wait(20);
            }
            this.context.players.title("战斗!");
            this.start();
        });
    }

    tpToStart() {
        const players = this.context.players.getAll().filter((p) => p.isValid);
        const startPoints = this.context.map.startPoints;
        for (let i = 0; i < startPoints.length && i < players.length; i++) {
            const block = Dimensions.Overworld.getBlock(
                Vector3Utils.add(startPoints[i], {
                    x: 0,
                    y: POFStatic.watingRoomOffsetY + 1,
                    z: 0,
                })
            );
            if (!block) return;
            players[i]?.player?.teleport(block.center());
        }
    }

    start() {
        this.transitionTo(PillarOfFortuneMainState);
    }
}
